#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

class Solution {
public:

    // 416. 分割等和子集
    // https://leetcode.cn/problems/partition-equal-subset-sum/
    bool canPartition(vector<int>& nums) {
        int n = nums.size();

        int sum = 0;
        for (auto num : nums)
            sum += num;
        if (sum & 1) return false;

        int target = sum / 2;
        vector<bool> dp(target+1);
        dp[0] = true;
        
        for (int i = 1; i<=n; ++i)
        {
            for (int j = target; j>=nums[i-1]; --j)
            {
                dp[j] = dp[j] || dp[j-nums[i-1]];
            }
        }

        return dp[target];
    }
};